package com.sgcc.pda.h5.db;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sgcc.pda.common.aidl.CommonCallback;
import com.sgcc.pda.common.aidl.CommonParams;
import com.sgcc.pda.common.aidl.CommonResults;
import com.sgcc.pda.data.IDBH5Manager;
import com.sgcc.pda.CommonResultsFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author superFan
 * date:: 2019/7/17/017 11:30
 * company:: beijing_hezhongweiqi
 * description:: 供H5应用操作数据库的binderutil
 */
public class DBH5BinderUtil {

    private static final String TAG = "DBH5BinderUtil";
    private static final String DBCODE="dbCode";
    private static final String DBNAME="dbName";
    private static final String TABLENAME="tableName";
    /**
     * 成功字段
     */
    public static final String SUCCESS = "success";
    static final String CLASS_NAME = "com.sgcc.pda.h5.db.DBH5BinderUtil";

    private IDBH5Manager mService = null;
    private boolean isBind = false;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IDBH5Manager.Stub.asInterface(service);
            isBind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBind = false;
            mService = null;
        }
    };

    private DBH5BinderUtil() {
    }

    private static class Instance {
        private static final DBH5BinderUtil mInstance = new DBH5BinderUtil();
    }

    /**
     * 单例
     */
    public static DBH5BinderUtil getInstance() {
        return Instance.mInstance;
    }

    /**
     * 绑定掌机基础服务提供接口的AIDL服务
     *
     * @param context
     * @return 是否绑定
     */
    public boolean bind(Context context) {
        if (isBind) {
            return isBind;
        }
        Intent intent = new Intent("com.sgcc.pda.data.AIDL_DATA_DB_SERVICE");
        intent.setPackage("com.sgcc.pda.mdrh.task.safe");
        try {
            isBind = context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        } catch (Exception e) {
            e.printStackTrace();
            isBind = false;
        }
        return isBind;
    }

    /**
     * 取消绑定掌机基础服务提供接口的AIDL服务
     *
     * @param context
     * @return 是否绑定
     */
    public void unbind(Context context) {
        try {
            context.unbindService(mConnection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据方法名执行对应的方法
     *
     * @param params   参数
     * @param callback 回调
     * @return 结果
     */
    public CommonResults execute(CommonParams params, CommonCallback callback) {
        String methodName = params.getMethodName();
        CommonResults commonResults;
        try {
            // 通过反射调用方法，方法参数与返回值必须统一成（CommonParams，CommonCallback，CommonResults）
            Method method = getClass().getMethod(methodName, CommonParams.class, CommonCallback.class);
            commonResults = (CommonResults) method.invoke(this, params, callback);
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.methodInvokeExceptionResults();
        }
        Log.d(TAG, "发送请求：" + params.toJSON() + "\n"
                + "返回结果：" + commonResults.toString());
        if (commonResults.getCode().equals("1")) {//只有调用数据库成功了,才能判断内部的数据库操作是否成功
            Map<String, Object> map = commonResults.getResults();
            int dbCode = (int) map.get(DBCODE);
            if (dbCode != 0) {
                commonResults = CommonResultsFactory.dbInvokeResult(commonResults);
            }
        }
        callback.callback(commonResults);
        return commonResults;
    }


    /**
     * 数据库初始化
     *
     * @param commonParams
     * @param commonCallback
     * @return
     * @throws RemoteException
     */
    public CommonResults initDatabase(CommonParams commonParams, CommonCallback commonCallback) throws RemoteException {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String dbEncrypPwd = commonParams.getString("dbEncrypPwd");//加密密码
        int version = commonParams.getInt("version");//版本号
        try {
            //2019/8/13/013 service为空 需要判断，并返回
            if (mService != null) {
                JSONObject initRes = new JSONObject(mService.initDatabase(dbName, dbEncrypPwd, version));
                commonResults.setResults(getDbOperate(initRes));
            } else {
                commonResults = CommonResultsFactory.customErrorResults("h5 bind 绑定失败");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
//        commonCallback.callback(commonResults);
        return commonResults;
    }

    /**
     * 创建数据表
     *
     * @param commonParams
     * @param commonCallback
     * @return
     * @throws RemoteException
     */
    public CommonResults create(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String tableMap = commonParams.getParam("tableMapJson").toString();//表字段定义《字典名，字典类型》
        Map map;
        try {
            map = new Gson().fromJson(tableMap, Map.class);
        } catch (Exception e) {
            map = new HashMap();
            e.printStackTrace();
        }
        try {
            JSONObject createRes = new JSONObject(mService.create(dbName, tableName, map));
//            commonResults.addResult("result", createRes);
            commonResults.setResults(getDbOperate(createRes));
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 插入一条数据
     * <p>
     * dbName    数据库名
     * tableName 数据表名
     * dataMap   插入的数据集《字段名，字段值》
     *
     * @return
     */
    public CommonResults insertOne(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String privateKey = commonParams.getString("privateKey");//记录中唯一属性的字段名
        String dataMapListJson = commonParams.getString("dataMapJson");//需要插入的单条数据
        try {
            JSONObject jsonObject = new JSONObject(dataMapListJson);
            Iterator<String> keys = jsonObject.keys();
            Map<String, String> map = new HashMap<>();
            while (keys.hasNext()) {
                String key = keys.next();
                map.put(key, jsonObject.getString(key));
            }
            JSONObject dropTableRes = new JSONObject(mService.insertOne(dbName, tableName, privateKey, map));
            commonResults.setResults(getDbOperate(dropTableRes));
//            commonResults.addResult("result", dropTableRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (JSONException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.parametersExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
//        commonCallback.callback(commonResults);
        return commonResults;
    }

    /**
     * 插入多条数据
     * <p>
     * dbName
     * tableName
     * dataMapList 需要插入的数据集 List<Map<String, String>>
     *
     * @return
     */
    public CommonResults insertList(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String privateKey = commonParams.getString("privateKey");//记录中唯一属性的字段名
        String dataMapListJson = commonParams.getString("dataMapListJson");//需要插入的数据集
        try {
            JSONObject dropTableRes = new JSONObject(mService.insertList(dbName, tableName, privateKey, dataMapListJson));
            commonResults.setResults(getDbOperate(dropTableRes));
//            commonResults.addResult("result", dropTableRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 删除表
     * <p>
     * dbName    数据库名
     * tableName 表名
     *
     * @return
     */
    public CommonResults dropTable(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        try {
            JSONObject dropTableRes = new JSONObject(mService.dropTable(dbName, tableName));
//            commonResults.addResult("result", dropTableRes);
            commonResults.setResults(getDbOperate(dropTableRes));
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 清空数据表
     * <p>
     * dbName    数据库名
     * tableName 表名
     *
     * @return
     */
    public CommonResults clear(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        try {
            JSONObject clearRes = new JSONObject(mService.clear(dbName, tableName));
            commonResults.setResults(getDbOperate(clearRes));
//            commonResults.addResult("result", clearRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 更新数据
     * <p>
     * dbName         数据库名
     * tableName      表名
     * dataMap        更新的数据 《字段名，更新的值》
     * whereCondition 条件语句  不带where
     *
     * @return
     */
    public CommonResults update(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String dataMapJson = commonParams.getString("dataMapJson");//更新数据
        String whereCondition = commonParams.getString("whereCondition");//查询条件
        try {
            JSONObject jsonObject = new JSONObject(dataMapJson);
            Map<String, String> dataMap = new HashMap<>();
            Iterator<String> keys = jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = jsonObject.getString(key);
                dataMap.put(key, value);
            }
            JSONObject updateRes = new JSONObject(mService.update(dbName, tableName, dataMap, whereCondition));
//            commonResults.addResult("result", updateRes);
            commonResults.setResults(getDbOperate(updateRes));
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (JSONException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.parametersExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 查询数据库：
     * <p>
     * dbName             数据库名
     * tableName          表名
     * selectColumnNames  查询列名
     * whereCondition     where条件语句(联合查询是,不同表的条件用分号分隔)
     * groupByColumnNames groupby 语句
     * haveingCondition   haveing语句
     * orderByCondition   orderby语句
     * limitCondition     limit语句
     *
     * @return 数据库信息json； key为字段名
     */
    public CommonResults query(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String selectColumnNames = commonParams.getString("selectColumnNames");//选择查询的字段列,逗号分隔
        String whereCondition = commonParams.getString("whereCondition");//where条件语句(联合查询不同表的条件用分号分隔)
        String groupByColumnNames = commonParams.getString("groupByColumnNames");//groupby条件语句,逗号分隔
        String haveingCondition = commonParams.getString("haveingCondition");//where条件having语句
        String orderByCondition = commonParams.getString("orderByCondition");//orderby语句
        String limitCondition = commonParams.getString("limitCondition");//limit语句

        try {
            String[] tableNameList = TextUtils.isEmpty(tableName) ?
                    new String[0] : tableName.split(",");
            String[] selectColumnNamesList = TextUtils.isEmpty(selectColumnNames) ?
                    new String[0] : selectColumnNames.split(",");
            String[] groupByColumnNamesList = TextUtils.isEmpty(groupByColumnNames) ?
                    new String[0] : groupByColumnNames.split(",");
            JSONObject rawQueryRes = null;
            if (tableNameList.length > 1) {
                String[] whereConditionList = TextUtils.isEmpty(whereCondition) ?
                        new String[tableNameList.length - 1] : whereCondition.split(";");
                rawQueryRes = new JSONObject(mService.joinQuery(dbName, Arrays.asList(tableNameList), Arrays.asList(selectColumnNamesList),
                        Arrays.asList(whereConditionList), Arrays.asList(groupByColumnNamesList), haveingCondition, orderByCondition, limitCondition));
            } else {
                rawQueryRes = new JSONObject(mService.query(dbName, tableName, Arrays.asList(selectColumnNamesList),
                        whereCondition, Arrays.asList(groupByColumnNamesList), haveingCondition, orderByCondition, limitCondition));
            }
            commonResults.setResults(getDbOperate(rawQueryRes));
//            commonResults.addResult("result", rawQueryRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;

    }

    /**
     * 删除数据
     * dbName            数据库名
     * tableName         表名
     * whereCondition where语句
     *
     * @return
     */
    public CommonResults delete(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String whereMapCondition = commonParams.getString("whereCondition");//where条件语句
        try {
            JSONObject rawExecSQLRes = new JSONObject(mService.delete(dbName, tableName, whereMapCondition));
            commonResults.setResults(getDbOperate(rawExecSQLRes));
//            commonResults.addResult("result", rawExecSQLRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 直接执行sql语句（支持查询）
     * <p>
     * dbName
     * sqlStr
     *
     * @return
     */
    public CommonResults rawQuerySQL(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String sqlStr = commonParams.getString("sqlStr");//sql语句
        try {
            JSONObject rawExecSQLRes = new JSONObject(mService.rawQuerySQL(dbName, sqlStr));
            commonResults.setResults(getDbOperate(rawExecSQLRes));
//            commonResults.addResult("result", rawExecSQLRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 直接执行sql语句（不支持查询）
     * <p>
     * dbName
     * sqlStr
     *
     * @return
     */
    public CommonResults rawExecSQL(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String sqlStr = commonParams.getString("sqlStr");//sql语句
        try {
            JSONObject rawExecSQLRes = new JSONObject(mService.rawExecSQL(dbName, sqlStr));
            commonResults.setResults(getDbOperate(rawExecSQLRes));
//            commonResults.addResult("result", rawExecSQLRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }


    /**
     * 获取数据库信息
     * <p>
     * dbName 数据库名
     *
     * @return 数据库信息json：version=版本号；path=路径; tableList=数据库表名(含有系统表sqlite_master)
     */
    public CommonResults getDBInfo(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        try {
            JSONObject dbInfo = new JSONObject(mService.getDBInfo(dbName));
            commonResults.setResults(getDbOperate(dbInfo));
//            commonResults.addResult("result", dbInfo);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 获取表结构信息
     *
     * @param commonParams
     * @param commonCallback
     * @return
     */
    public CommonResults getTableStructureInfo(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        try {
            JSONObject tableStructureInfo = new JSONObject(mService.getTableStructureInfo(dbName, tableName));
            commonResults.setResults(getDbOperate(tableStructureInfo));
//            commonResults.addResult("result", tableStructureInfo);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 判断数据库中的 表是否存在
     *
     * @param commonParams
     * @param commonCallback
     * @return
     */
    public CommonResults isTableExist(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        try {
            JSONObject isTableExist = new JSONObject(mService.isTableExist(dbName, tableName));
            commonResults.setResults(getDbOperate(isTableExist));
//            commonResults.addResult("result", isTableExist);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 修改表名
     *
     * @param commonParams
     * @param commonCallback
     * @return
     */
    public CommonResults reNameTable(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String oldTableName = commonParams.getString("oldTableName");//旧表名
        String newTableName = commonParams.getString("newTableName");//新表名
        try {
            JSONObject reNameRes = new JSONObject(mService.reNameTable(dbName, oldTableName, newTableName));
            commonResults.setResults(getDbOperate(reNameRes));
//            commonResults.addResult("result", reNameRes);
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 添加 表 字段
     *
     * @return
     */
    public CommonResults addColumn(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        String dbName = commonParams.getString(DBNAME);//数据库名
        String tableName = commonParams.getString(TABLENAME);//表名
        String addColumnMapJson = commonParams.getString("addColumnMapJson");//添加的列json； key=列名，value=列属性
        try {
            JSONObject jsonObject = new JSONObject(addColumnMapJson);
            Map<String, String> addColumonMap = new HashMap<>();
            Iterator<String> keys = jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = jsonObject.getString(key);
                addColumonMap.put(key, value);
            }
            JSONObject addColumnRes = new JSONObject(mService.addColumn(dbName, tableName, addColumonMap));
            commonResults.setResults(getDbOperate(addColumnRes));
//            commonResults.addResult("result", addColumnRes);
        } catch (JSONException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.parametersExceptionResults();
        } catch (RemoteException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.remoteExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 执行事务
     */
    public CommonResults transaction(CommonParams commonParams, CommonCallback commonCallback) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        final String dbName = commonParams.getString(DBNAME);//数据库名
        String sqlArrayJson = commonParams.getString("sqlArrayJson");//sql 语句
        try {
            JSONArray jsonArray = new JSONArray(sqlArrayJson);
            final List<String> sqlList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                sqlList.add(jsonArray.getJSONObject(i).getString("sql"));
            }
            JSONObject transaction = null;
            transaction = new JSONObject(mService.transaction(dbName, sqlList));
            commonResults.setResults(getDbOperate(transaction));
//            commonResults.addResult("result", transaction);
        } catch (JSONException e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.parametersExceptionResults();
        } catch (Exception e) {
            e.printStackTrace();
            commonResults = CommonResultsFactory.customErrorResults(e.getMessage());
        }
        return commonResults;
    }

    /**
     * 关闭指定数据库
     * <p>
     * dbName 数据库名
     *
     * @throws RemoteException
     */
    public void closeDatabase(String dbName) throws RemoteException {
        mService.closeDatabase(dbName);
    }

    /**
     * 关闭所有数据库
     */
    public void closeAllSqliteHelper() {
        try {
            if (mService != null) {
                mService.closeAllSqliteHelper();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private Map<String, Object> getDbOperate(JSONObject jsonObject) {
        Map<String, Object> map = null;
        try {
            map = new HashMap<>();
            boolean isSuccess = jsonObject.getBoolean("isSuccess");
            if (isSuccess) {
                map.put(DBCODE, 0);
                map.put("dbMsg", jsonObject.get("result"));
            } else {
                map.put(DBCODE, -1);
                map.put("dbMsg", jsonObject.get("reason"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }
}
